<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线商城</title>
    <style>
        .product-list{
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;
        }
        .product{
            width: 300px;
            padding: 20px;
            border: 1px solid #ddd;
            margin: 20px;
            box-shadow: 0 0 10 rgba(0,0,0,0.1);
        }
        .product img{
            width: 100%;
        }
        .cart{
            padding: 20px;
            border:1px solid #ddd;
            width: 300px;
            margin: 20px;
        }
        .cart h3{
            margin-top: 0;
        }

    </style>
</head>
<body>
    <h2>在线商城</h2>
    <div class="product-list">
        <div class="product">
            <img src="sg/0x300a0a0 (1).jpg" alt="Product1">
            <h3>产品1</h3>
            <p>价格:$50</p>
            <button id="add-to-cart-1">加入购物车</button>
        </div>
        <div class="product">
            <img src="sg/0x300a0a0 (2).jpg" alt="Product2">
            <h3>产品2</h3>
            <p>价格:$70</p>
            <button id="add-to-cart-1">加入购物车</button>
        </div>
        <!-- 您可以再这加上更多的产品 -->
         <div class="cart">
            <h3>购物车</h3>
            <ul id="cart-items"></ul>
         </div>
    </div>
    <script>
        //获取商品添加到购物车的按钮
        const addToCartButtons = document.querySelectorAll('[id^="add-to-cart-"]');
        //为每个添加到购物车的按钮添加点击事件处理函数
        addToCartButtons.forEach(button => {
            button.addEventListener('click',function(){
                const productName = this.parentElement.querySelector('h3').textContent;
                const cartItimeList = document.getElementById('cart-items');
                //创建新的列表项并添加到购物车
                const listItem = document.createElement('li');
                listItem.textContent = productName + '以添加到购物车';
                cartItimeList.appendChild(listItem);
            });
        });
    </script>
</body>
</html>